﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.XPath;

namespace HtmlEditor
{
    public partial class ColorPicker : UserControl
    {
        private bool IsRaiseColorChangedEvent = true;
        public event EventHandler<PropertyChangedEventArgs<Color>> SelectedColorChanged;
        public ColorPicker()
        {
            InitializeComponent();
            InitColors();
        }
        public Color SelectedColor
        {
            get { return (Color)GetValue(SelectedColorProperty); }
            set { SetValue(SelectedColorProperty, value); }
        }
        public static readonly DependencyProperty SelectedColorProperty =
            DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorPicker),
                new UIPropertyMetadata(Colors.Transparent, new PropertyChangedCallback(OnSelectedColorPropertyChanged)));
        private static void OnSelectedColorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ColorPicker control = sender as ColorPicker;
            if (control != null && control.SelectedColorChanged != null && control.IsRaiseColorChangedEvent)
                control.SelectedColorChanged(control, PropertyChangedEventArgs<Color>.Create((Color)e.NewValue));
        }
        public void Reset()
        {
            IsRaiseColorChangedEvent = false;
            StandardColors.SelectedItem = null;
            SelectedColor = Color.FromRgb(0x01, 0x01, 0x01);
            IsRaiseColorChangedEvent = true;
        }
        private void InitColors()
        {
            List<MyItem> ls = new List<MyItem>()
            {
                 new MyItem() {  Name ="#E53333",   Value ="#E53333"},
                 new MyItem() {  Name ="#E56600",   Value ="#E56600"},
                 new MyItem() {  Name ="#FF9900",   Value ="#FF9900"},
                 new MyItem() {  Name ="#64451D",   Value ="#64451D"},
                 new MyItem() {  Name ="#DFC5A4",   Value ="#DFC5A4"},
                 new MyItem() {  Name ="#FFE500",   Value ="#FFE500"},

                 new MyItem() {  Name ="#009900",   Value ="#009900"},
                 new MyItem() {  Name ="#006600",   Value ="#006600"},
                 new MyItem() {  Name ="#99BB00",   Value ="#99BB00"},
                 new MyItem() {  Name ="#B8D100",   Value ="#B8D100"},
                 new MyItem() {  Name ="#60D978",   Value ="#60D978"},
                 new MyItem() {  Name ="#00D5FF",   Value ="#00D5FF"},
   
                 new MyItem() {  Name ="#337FE5",   Value ="#337FE5"},
                 new MyItem() {  Name ="#003399",   Value ="#003399"},
                 new MyItem() {  Name ="#4C33E5",   Value ="#4C33E5"},
                 new MyItem() {  Name ="#9933E5",   Value ="#9933E5"},
                 new MyItem() {  Name ="#CC33E5",   Value ="#CC33E5"},
                 new MyItem() {  Name ="#EE33EE",   Value ="#EE33EE"},

                 new MyItem() {  Name ="#FFFFFF",   Value ="#FFFFFF"},
                 new MyItem() {  Name ="#CCCCCC",   Value ="#CCCCCC"},
                 new MyItem() {  Name ="#999999",   Value ="#999999"},
                 new MyItem() {  Name ="#666666",   Value ="#666666"},
                 new MyItem() {  Name ="#333333",   Value ="#333333"},
                 new MyItem() {  Name ="#000000",   Value ="#000000"}
            };
            if (ls.Count == 0)
                return;
            StandardColors.ItemsSource = ls;
        }
        private void HandleSelect(object sender, MouseButtonEventArgs e)
        {
            ListBoxItem lbi = sender as ListBoxItem;

            if (lbi == null)
                return;
            string val = ((MyItem)(lbi).Content).Value;
            SelectedColor = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(val);
        }
    }
    public class PropertyChangedEventArgs<T> : EventArgs
    {
        private PropertyChangedEventArgs() { }

        public T Value { get; private set; }

        public static PropertyChangedEventArgs<T> Create(T val)
        {
            return new PropertyChangedEventArgs<T>() { Value = val };
        }
    }
    /// <summary>
    /// 添加项
    /// </summary>
    public struct MyItem
    {
        public string Name { set; get; }
        public string Value { set; get; }
    }
}
